<?php
namespace Beerus\Modules\WikiDevidepackage\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Doraemon\model\DevidePackage\PicPackage;
use Doraemon\model\DevidePackage\PackageRelatedInfo;
use Doraemon\model\DevidePackage\UserPackage;
use Biz\pockets\excel\DownExcel;

class WikiDevidePackageController extends Controller
{
    const PAGE_SIZE = 20;

    const OPERATE_ROLE = ['high', 'normal'];

    const COLLOCATION_FILE_WIKI = '/home/adm_rsync_dir/package_collocation_wiki.txt';

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    private function _filterWhere($params){
        if(isset($params['_columns_'])){
            unset($params['_columns_']);
        }
        if(isset($params['_pjax'])){
            unset($params['_pjax']);
        }
        $page = isset($params['page']) ? $params['page'] : 1;
        $pageSize = isset($params['per_page']) ? $params['per_page'] : self::PAGE_SIZE;
        $limit = [($page - 1) * $pageSize, $pageSize];
        unset($params['page']);
        $where = [];
        foreach ($params as $_k => &$_v) {
            if ($_v <> '') {
                $where[$_k] = $_v;
            }
        }
        $data = [
            'LIMIT' => $limit,
        ];

        if (!empty($where)) {
            $data['AND'] = $where;
        }

        return $data;
    }

    //校验用户角色
    public function checkRole()
    {
        $package_manager = Admin::user()->isRole('package_manager');
        $package_high_operate = Admin::user()->isRole('package_high_operate');
        $package_normal_operate = Admin::user()->isRole('package_normal_operate');
        if($package_manager) {
            return 'manage';
        }
        if($package_high_operate) {
            return 'high';
        }
        if($package_normal_operate) {
            return 'normal';
        }
        return 'normal';
    }

    //获取对应tab
    public function getTabs($tab_id, $role)
    {
        $tab = new Tab();
        switch($role) {
            case 'manage':
                $tab->addLink('未领取', '/admin/wikidevidepackage/index/1', $tab_id == 1);
                $tab->addLink('待审核', '/admin/wikidevidepackage/index/6', $tab_id == 6);
                $tab->addLink('已领取待提交', '/admin/wikidevidepackage/index/3', $tab_id == 3);
                $tab->addLink('已完成', '/admin/wikidevidepackage/index/5', $tab_id == 5);
                $tab->addLink('配置项', '/admin/wikidevidepackage/index/7', $tab_id == 7);
                $tab->addLink('统计信息', '/admin/wikidevidepackage/index/8', $tab_id == 8);
                break;
            case 'high':
            case 'normal':
                $tab->addLink('领取任务', '/admin/wikidevidepackage/index/1', $tab_id == 1);
                $tab->addLink('做任务', '/admin/wikidevidepackage/index/3', $tab_id == 3);
                $tab->addLink('待审核', '/admin/wikidevidepackage/index/6', $tab_id == 6);
                $tab->addLink('已完成', '/admin/wikidevidepackage/index/5', $tab_id == 5);
                break;
        }

        return $tab;
    }

    public function index(Request $request, Content $content, $tab_id)
    {

        $request = $request->all();
        $request = self::_filterWhere($request);
        $role = $this->checkRole();
        $tab = $this->getTabs($tab_id, $role);
        return $content
            ->header('wiki审核分包管理')
            ->breadcrumb(
                ['text' => 'wiki审核分包管理', 'url' => '/wikidevidepackage/'.$tab_id]
            )
            ->body($tab)->row(self::listAction($request, $role, $tab_id));

    }

    public function listAction($request = [], $role, $tab_id)
    {
        $list = [];
        switch($tab_id) {
            case 1:
                //待领取
                $listGrid = $this->unClaimList($request, $role);
                $list = $listGrid->render();
                break;
            case 3:
                //待提交
                $listGrid = $this->unSubmitList($request, $role);
                $list = $listGrid->render();
                break;
            case 5:
                //已完成
                $listGrid = $this->finishList($request, $role);
                $list = $listGrid->render();
                break;
            case 6:
                //待审核
                $listGrid = $this->unCheckList($request, $role);
                $list = $listGrid->render();
                break;
            case 7:
                //配置项
                $listGrid = $this->collocationList($request, $role);
                $list = $listGrid->render();
                break;
            case 8:
                //统计信息
                $listGrid = $this->statisticsList($request, $role);
                $list = $listGrid->render();
                break;
        }
        return $list;
    }

    //待领取列表
    public function unClaimList($request, $role)
    {
        //待领取
        $package_infos = PicPackage::getUnClaimedPackage(PicPackage::PACKAGE_TYPE_WIKI);
        if($package_infos && is_array($package_infos)) {
            foreach($package_infos as $k=>$package_info) {
                if(in_array($role, ['normal']) && $package_info['show_time'] >= date('Y-m-d H:i:s', time())) {
                    unset($package_infos[$k]);
                }
            }
        }
        if(in_array($role, ['high', 'normal'])) {
            $package_infos = array_slice($package_infos, 0, 7);
        }
        $gridModel = new pGridDataModel($package_infos, count($package_infos), []);
        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $grid->disableExport();
            $grid->disableFilter();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->disablePagination();

            $grid->column('id', '任务包id');
            $grid->column('addtime', '打包时间');
            $grid->column('period', '任务有效期')->display(function() {
                return $this->period."小时";
            });
            $grid->column('wiki_num', 'wiki数');
            $role = $this->checkRole();
            if(in_array($role, ['high', 'normal'])) {
                $grid->actions(function (Grid\Displayers\Actions $actions){
                    $actions->disableEdit();
                    $actions->disableDelete();
                    $actions->disableView();
                    $actions->prepend('<a class="fa reload-data fa-get-pocket received-package" data-id="'.$actions->row->id.'" href="javascript:;" >领取任务</a><br>');
                });
            } else {
                $grid->column('show_time', '普通操作员开始展示时间');
                $grid->actions(function (Grid\Displayers\Actions $actions){
                    $actions->disableEdit();
                    $actions->disableDelete();
                    $actions->disableView();
                });
            }
            Admin::script($this->receivedPopup());
        });
        return $listGrid;
    }

    //领取任务
    public function receivePackage(Request $request)
    {
        $data = [
            'code' => 0,
            'msg' => '未知错误'
        ];
        $request = $request->all();
        $package_id = $request['package_id'];//任务包id
        $admin_id = Admin::user()->id;
        //当前任务包是否已经被认领
        $package_info  = UserPackage::getPackageClaimant($package_id);
        $package_base_info = PicPackage::getPackageBaseInfo($package_id);
        if(empty($package_info)) {
            //当前用户是否有权限认领任务包
            $role = $this->checkRole();
            if(in_array($role, self::OPERATE_ROLE)) {
                //领取任务包
                PicPackage::editPackageClaimant($package_id, [
                    'package_status' => PicPackage::STATUS_RECEIVED,
                    'edittime' => date('Y-m-d H:i:s', time())
                ]);
                $data = [
                    "claimant" => $admin_id,
                    "package_id" => $package_id,
                    "period_time" => date('Y-m-d H:i:s', time() + $package_base_info['period']*3600),
                    "wiki_num" => (int) $package_base_info['wiki_num'],
                    "package_type" => $package_base_info['package_type'],
                    "addtime" => date('Y-m-d H:i:s', time())
                ];
                UserPackage::addUserPackage($data);
                PackageRelatedInfo::updatePackageClaimant($package_id, $admin_id);
                //校验认领人
                $claimant = UserPackage::getPackageClaimant($package_id)['claimant'];
                if($admin_id != $claimant) {
                    PackageRelatedInfo::updatePackageClaimant($package_id, $claimant);
                }

                $data = [
                    'code' => 1,
                    'msg' => '任务包领取成功'
                ];
            } else {
                $data = [
                    'code' => 0,
                    'msg' => '当前用户无权限领取任务包'
                ];
            }
        } else {
            $data = [
                'code' => 0,
                'msg' => '此任务包已处于不可领取状态'
            ];
        }
        return $data;
    }
    //领取任务弹窗
    public function receivedPopup()
    {
        return <<<SCRIPT
           
            $('.received-package').unbind('click').click(function() {
            var id = $(this).data('id');
            swal({
                title: "确认领取这个任务嘛？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认领取",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    $.post('receivedpackage', {'package_id':id}, function (data) {
                        $.pjax.reload('#pjax-container');
                        if(data.code == 1) {
                            swal(data.msg, '', 'success');
                        } else {
                            swal(data.msg, '', 'error');
                        }
                    });
                   
                }
            });
        });
SCRIPT;
    }

    //待提交列表
    public function unSubmitList($request, $role)
    {
        //待提交列表
        $admin_id = Admin::user()->id;
        $role = $this->checkRole();
        if(in_array($role, ['manage'])) {
            $package_infos = UserPackage::getPackageList(UserPackage::STATUS_UNSUBMIT, [], PicPackage::PACKAGE_TYPE_WIKI);
        } else {
            $package_infos = UserPackage::getPackageListByClaimant($admin_id, UserPackage::STATUS_UNSUBMIT, [], PicPackage::PACKAGE_TYPE_WIKI);
        }

        $gridModel = new pGridDataModel($package_infos, count($package_infos), []);
        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $role = $this->checkRole();
            $grid->disableExport();
            $grid->disableFilter();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->disablePagination();

            $grid->column('id', '领取关联id');
            $grid->column('package_id', '任务包id');
            $grid->column('addtime', '任务领取时间');
            $grid->column('period_time', '任务到期时间')->display(function() {
                $diff = floor((strtotime($this->period_time) - time())/3600);
                $color = '';
                if($diff < 10) {
                     $color = 'color:red;';
                }
                return "<span>".$this->period_time."</span><br/><span style='$color'>距离到期还有约".$diff."个小时</span>";
            });
            $grid->column('wiki_num', 'wiki数');

            if(in_array($role, ['high', 'normal'])) {
                $grid->actions(function (Grid\Displayers\Actions $actions){
                    $actions->disableEdit();
                    $actions->disableDelete();
                    $actions->disableView();
                    $actions->prepend('<a class="fa reload-data fa-hand-peace-o submit-package" href="javascript:;" data-id="'.$actions->row->package_id.'">提交任务</a>');

                    $actions->prepend('<a class="fa reload-data fa-hand-paper-o continue-package" href="/admin/wiki/pack_list?package_id='.$actions->row->package_id.'" >做任务</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
                });
            } else {
                $grid->column('note_num', 'note数');
                $grid->column('claimant', '认领人')->display(function() {
                    return "id：" . Admin::user()->find($this->claimant)->id . "<br/>用户名：" . Admin::user()->find($this->claimant)->name;
                });
                $grid->actions(function (Grid\Displayers\Actions $actions){
                    $actions->disableEdit();
                    $actions->disableDelete();
                    $actions->disableView();
                    $actions->prepend('<a class="fa reload-data fa-hand-paper-o continue-package" href="/admin/wiki/pack_list?package_id='.$actions->row->package_id.'" >查看任务详情</a>');
                });
            }

            Admin::script($this->submitPopup());
        });
        return $listGrid;
    }

    //提交任务弹窗
    public function submitPopup()
    {
        return <<<SCRIPT
           
            $('.submit-package').unbind('click').click(function() {
            var id = $(this).data('id');
            swal({
                title: "确认要将这个任务提交审核吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认提交",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    $.post('submitpackage', {'package_id':id}, function (data) {
                        $.pjax.reload('#pjax-container');
                        if(data.code == 1) {
                            swal(data.msg, '', 'success');
                        } else {
                            swal(data.msg, '', 'error');
                        }
                    });
                   
                }
            });
        });
SCRIPT;
    }

    //提交任务
    public function submitPackage(Request $request)
    {
        $data = [
            'code' => 0,
            'msg' => '未知错误'
        ];
        $request = $request->all();
        $package_id = $request['package_id'];
        $package_info = UserPackage::getPackageClaimant($package_id);
        //校验提交人
        if($package_info['claimant'] != Admin::user()->id) {
            $data = [
                'code' => 0,
                'msg' => '无权限，提交人与任务包领取者不符'
            ];
            return $data;
        }
        //校验任务包状态
        if(!in_array($package_info['checkstatus'], UserPackage::OPERATE_STATUS)) {
            $data = [
                'code' => 0,
                'msg' => '任务包目前处于不可提交状态'
            ];
            return $data;
        }
        //更新必要字段
        if($package_info['check_status'] == UserPackage::STATUS_UNSUBMIT) {
            $new_status  = UserPackage::STATUS_UNCHECK;
        } else {
            $new_status  = UserPackage::STATUS_UNREPEAT;
        }

        $hasChecks = PackageRelatedInfo::getCheckWikiByPackageId($package_id);
        $check_num = array_column($hasChecks, 'wiki_id');
        $rs = [
            "check_status" => $new_status,
            "edittime" => date('Y-m-d H:i;s' ,time()),
            "check_wiki_num" => (int) count($check_num),
        ];
        UserPackage::updateUserPackage($package_info['id'], $rs);
        $data = [
            'code' => 1,
            'msg' => '任务包提交成功'
        ];
        return $data;
    }

    //待审核列表
    public function unCheckList($request, $role)
    {
        $admin_id = Admin::user()->id;
        if(in_array($role, ['manage'])) {
            $package_infos = UserPackage::getPackageList([UserPackage::STATUS_UNCHECK, UserPackage::STATUS_UNREPEAT], [], PicPackage::PACKAGE_TYPE_WIKI);
        } else {
            $package_infos = UserPackage::getPackageListByClaimant($admin_id, [UserPackage::STATUS_UNCHECK, UserPackage::STATUS_UNREPEAT], [], PicPackage::PACKAGE_TYPE_WIKI);
        }

        $gridModel = new pGridDataModel($package_infos, count($package_infos), []);
        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $role = $this->checkRole();
            $grid->disableExport();
            $grid->disableFilter();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->disablePagination();

            $grid->column('id', '领取关联id');
            $grid->column('package_id', '任务包id');
            $grid->column('check_status', '任务状态')->replace(UserPackage::CHECK_STATUS);
            $grid->column('addtime', '任务领取时间');
            $grid->column('edittime', '任务提交时间');
            $grid->column('submit_type', '提交类型')->replace(UserPackage::SUBMIT_TYPE);
            $grid->column('wiki_num', 'wiki数');
            $grid->column('check_wiki_num', '审核的wiki数');

            if(in_array($role, ['manage'])) {
                $grid->column('adm_remark', '整包备注')->editable();
                $grid->column('claimant', '认领人')->display(function() {
                    return "id：" . Admin::user()->find($this->claimant)->id . "<br/>用户名：" . Admin::user()->find($this->claimant)->name;
                });
                $grid->actions(function (Grid\Displayers\Actions $actions){
                    $actions->disableEdit();
                    $actions->disableDelete();
                    $actions->disableView();

                    $actions->prepend('<a class="fa reload-data fa-check finish-package" href="javascript:;" data-id="'.$actions->row->id.'">标记完成</a>&nbsp;&nbsp;&nbsp;&nbsp;');

                    $actions->prepend('<a class="fa reload-data fa-calendar" href="/admin/wiki/pack_list?package_id='.$actions->row->package_id.'">审核详情</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
                });
            } else {
                $grid->column('adm_remark', '整包备注');
                $grid->actions(function (Grid\Displayers\Actions $actions){
                    $actions->disableEdit();
                    $actions->disableDelete();
                    $actions->disableView();
                });
            }

            Admin::script($this->finishPopup());
        });
        return $listGrid;
    }

    //整包备注
    public function editRemark(Request $request, $tab_id, $id)
    {
        $request = $request->all();
        $data['adm_remark'] = $request['value'];
        $data['edittime'] = date('Y-m-d H:i:s', time());
        $rs = UserPackage::updateUserPackage($id, $data);
        $package_id = UserPackage::getPackageInfo($id)['package_id'];
        if($package_id && $data['adm_remark']) {
            PackageRelatedInfo::updatePackageProblem($package_id);
        }
        if($rs) {
            return response()->json([
                'status' => true,
                'message' => "修改成功",
            ]);
        }
        return response()->json([
            'status' => false,
            'message' => "修改失败",
        ]);
    }


    //标记完成弹窗
    public function finishPopup()
    {
        return <<<SCRIPT
           
            $('.finish-package').unbind('click').click(function() {
            var id = $(this).data('id');
            swal({
                title: "确认要将这个任务标记完成吗？此操作不可逆哦",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认已完成",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    $.post('finishpackage', {'id':id}, function (data) {
                        $.pjax.reload('#pjax-container');
                        if(data.code == 1) {
                            swal(data.msg, '', 'success');
                        } else {
                            swal(data.msg, '', 'error');
                        }
                    });
                   
                }
            });
        });
SCRIPT;
    }

    //任务包标记完成
    public function finishPackage(Request $request)
    {
        $request = $request->all();
        $id = $request['id'];

        $pickage_id = UserPackage::getPackageInfo($id)['package_id'];
        //计算wiki总数和已审核总数
        $check_wiki_num = count(PackageRelatedInfo::getCheckWikiByPackageId($pickage_id));

        $data = [
            'edittime' => date('Y-m-d H:i:s', time()),
            'finish_time' => date('Y-m-d H:i:s', time()),
            'check_status' => UserPackage::STATUS_FINISH,
            'check_user' => Admin::user()->id,
            'check_wiki_num' => (int) $check_wiki_num
        ];
        $rs = UserPackage::updateUserPackage($id, $data);
        if($rs) {
            return [
                'code' => 1,
                'msg' => '任务包已标记完成'
            ];
        }
        return [
            'code' => 0,
            'msg' => '任务包标记完成失败'
        ];
    }

    //已完成列表
    public function finishList($request, $role)
    {
        $admin_id = Admin::user()->id;
        if(in_array($role, ['manage'])) {
            $package_all = UserPackage::getPackageList(UserPackage::STATUS_FINISH, [], PicPackage::PACKAGE_TYPE_WIKI);
            $package_infos = UserPackage::getPackageList(UserPackage::STATUS_FINISH, $request['LIMIT'], PicPackage::PACKAGE_TYPE_WIKI);
        } else {
            $package_infos = UserPackage::getPackageListByClaimant($admin_id, UserPackage::STATUS_FINISH, $request['LIMIT'], PicPackage::PACKAGE_TYPE_WIKI);
            $package_all = UserPackage::getPackageListByClaimant($admin_id, UserPackage::STATUS_FINISH, PicPackage::PACKAGE_TYPE_WIKI);
        }

        $gridModel = new pGridDataModel($package_infos, count($package_all), [
            'perPage' => $request['per_page'] ? : 20
        ]);
        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $role = $this->checkRole();
            $grid->disableExport();
            $grid->disableFilter();
            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->column('id', '领取关联id');
            $grid->column('package_id', '任务包id');
            $grid->column('addtime', '任务领取时间');
            $grid->column('finish_time', '任务完成时间');
            $grid->column('submit_type', '提交类型')->replace(UserPackage::SUBMIT_TYPE);
            $grid->column('wiki_num', '包含wiki总数');
            $grid->column('check_wiki_num', '已审核总数');
            $grid->column('adm_remark', '整包备注');
            if(in_array($role, ['manage'])) {
                $grid->column('claimant', '认领人')->display(function() {
                    return "id：" . Admin::user()->find($this->claimant)->id . "<br/>用户名：" . Admin::user()->find($this->claimant)->name;
                });
            }
        });
        return $listGrid;
    }

    //统计列表
    public function statisticsList($request, $role)
    {

        $url = $_SERVER["REQUEST_URI"];
        $data_url = explode('?',$url);
        $export_url = $data_url[1];

        $where = [
            'finish_time' => $request['AND']['finish_time'],
            'claimant' => $request['AND']['claimant']
        ];
        $package_infos = UserPackage::getPackags($where, PicPackage::PACKAGE_TYPE_WIKI);

        $gridModel = new pGridDataModel($package_infos, count($package_infos), []);
        $listGrid = Admin::grid($gridModel, function (Grid $grid) use ($gridModel, $export_url) {
            $grid->disableExport();
            $grid->expandFilter();
            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->disablePagination();

            $grid->tools(function ($tools) use ($export_url) {
                $url = url('admin/wikidevidepackage/exportdata?'.$export_url);
                $button = '<div class="btn-group download" style="margin-right: 10px">
    <a class="btn btn-sm btn-twitter" title="导出" target="_blank" href="'.$url.'"><i class="fa fa-download"></i><span class="hidden-xs"> 导出</span></a>
</div>';
                $tools->append($button);
            });


            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->dropDown('time', [
                    'finish_time' => '任务完成时间'
                ])->betweenDatetime();

                $filter->dropDown('search_id', [
                    'claimant' => '领取人id'
                ]);
            });

            $grid->column('id', '领取关联id');
            $grid->column('package_id', '任务包id');
            $grid->column('claimant', '认领人')->display(function() {
                return "id：" . Admin::user()->find($this->claimant)->id . "<br/>用户名：" . Admin::user()->find($this->claimant)->name;
            });
            $grid->column('addtime', '任务领取时间');
            $grid->column('finish_time', '任务完成时间');
            $grid->column('wiki_num', '包含wiki数');
            $grid->column('check_wiki_num', '已审核的wiki数');
            $grid->column('check_user', '审核人')->display(function() {
                return "id：" . Admin::user()->find($this->check_user)->id . "<br/>用户名：" . Admin::user()->find($this->check_user)->name;
            });

        });
        return $listGrid;
    }

    //导出数据
    public function exportData(Request $request)
    {
        $request = $request->all();
        $where = [
            'finish_time' => $request['finish_time'],
            'claimant' => $request['claimant']
        ];
        $package_infos = UserPackage::getPackags($where, PicPackage::PACKAGE_TYPE_WIKI);
        if($package_infos && is_array($package_infos)) {
            foreach($package_infos as $k=>$package_info) {
                $package_infos[$k]['claimant'] = "id：" . Admin::user()->find($package_infos[$k]['claimant'])->id . " 用户名：" . Admin::user()->find($package_infos[$k]['claimant'])->name;
                $package_infos[$k]['check_user'] = "id：" . Admin::user()->find($package_infos[$k]['check_user'])->id . " 用户名：" . Admin::user()->find($package_infos[$k]['check_user'])->name;
                unset($package_infos[$k]['check_status']);
                unset($package_infos[$k]['period_time']);
                unset($package_infos[$k]['submit_type']);
                unset($package_infos[$k]['adm_remark']);
                unset($package_infos[$k]['edittime']);
                unset($package_infos[$k]['pic_num']);
                unset($package_infos[$k]['note_num']);
                unset($package_infos[$k]['hastag_pic_num']);
                unset($package_infos[$k]['tag_num']);
                unset($package_infos[$k]['problem_pic_num']);
                unset($package_infos[$k]['package_type']);
            }
        }

        $reason = 'pic标签分包数据';
        $header = ['id', '兼职人员', '任务包id','审核人', '任务领取时间', '任务完成时间', 'wiki数','已审核wiki数'];
        $letter = range('A', 'K');
        $excelObj = new DownExcel();
        $excelObj->setFileName($reason);
        $excelObj->setSheetTitle($reason);
        //设置列宽
        $widths = ['A' => 25, 'B' => 25, 'C' => 25, 'D' => 25, 'E' => 60, 'F' => 60];
        $excelObj->setWidth($widths);
        //默认单元格居中
        $excelObj->setDefaultStyleAlignment();
        $excelObj->freezePane('A1');//冻结窗口
        $excelObj->setTextFormat('A');
        $excelObj->setTextFormat('B');
        foreach ($header as $key => $value) {
            //设置字体
            $excelObj->setFrontName($letter[$key]);
            //设置加粗
            $excelObj->setFrontBold("$letter[$key]1");
            //设置大小
            $excelObj->setFrontSize($letter[$key]);
        }
        $excelObj->create($package_infos, $header);
    }


    //配置列表
    public function collocationList($request, $role)
    {
        $data = [];
        if(file_exists(self::COLLOCATION_FILE_WIKI)) {
            $data = json_decode(file_get_contents(self::COLLOCATION_FILE_WIKI), true);
        }

        $gridModel = new pGridDataModel($data, count($data), []);
        $listGrid = Admin::grid($gridModel, function (Grid $grid) {
            $grid->disableExport();
            $grid->expandFilter();
            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->disablePagination();
            $grid->disableFilter();
            $grid->column('wiki_num', '任务包中wiki数')->editable()->help('请输入小于300的正整数哦');
            $grid->column('pre_hour', '优质兼职提前多少小时可见任务包')->editable()->help('请输入10以下的正整数哦');
            $grid->column('period_hour', '任务包有效期')->editable()->help('正整数，单位为小时，从领取任务开始计算时间');
        });
        return $listGrid;
    }

    public function collocation(Request $request)
    {
        if(file_exists(self::COLLOCATION_FILE_WIKI)) {
            $data = json_decode(file_get_contents(self::COLLOCATION_FILE_WIKI), true);
            $request = $request->all();
            $data[0][$request['name']] = (int) $request['value'];
            file_put_contents(self::COLLOCATION_FILE_WIKI, json_encode($data));
            return response()->json([
                'status' => true,
                'message' => "修改成功"
            ]);
        }
        return response()->json([
            'status' => false,
            'message' => "修改失败"
        ]);
    }

}